<template>
	<view>
		<view class="tui-keyboard-mask" :class="[show?'tui-mask-show':'']" v-if="mask" @tap="handleClose"></view>
		<view class="tui-keyboard" :class="{'tui-keyboard-radius':radius,'tui-keyboard-action':action,'tui-keyboard-show':show}">
			<slot></slot>
			<view class="tui-keyboard-grids">
				<!--{{(index==9 || index==10 || index==11)?'tui-grid-bottom':''}}-->
				<view class="tui-keyboard-grid" :class="{'tui-bg-gray':index==9 || index==11}" v-for="(item,index) in itemList"
				 :key="index" hover-class="tui-keyboard-hover" :hover-stay-time="150" @tap="handleClick" :data-index="index">
					<view v-if="index<11" class="tui-keyboard-item" :class="{'tui-fontsize-32':index==9}">{{getKeyBoard(index,action)}}</view>
					<view v-else class="tui-keyboard-item">
						<view class="tui-icon tui-keyboard-delete"></view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "tuiKeyboard",
		props: {
			//是否需要mask
			mask: {
				type: Boolean,
				default: true
			},
			//控制键盘显示
			show: {
				type: Boolean,
				default: false
			},
			//是否直接显示，不需要动画，一般使用在锁屏密码
			action: {
				type: Boolean,
				default: true
			},
			//是否带圆角
			radius: {
				type: Boolean,
				default: false
			},
			isPwd:{
				type: Boolean,
				default: true
			},
			// 输入框长度
			numberlen:{
				type: Number,
				default: 6
			}
		},
		data() {
			return {
				itemList: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
				numberArr: [],
				pwdArr: [],
				valArr:[],
			};
		},
		created() {
			for(let i = 0;i<this.numberlen;i++){
				this.pwdArr.push('')
				this.valArr.push('')
			}
		},
		methods: {
			getKeyBoard: function(index, action) {
				var content = index + 1;
				if (index == 9) {
					content = action ? "取消" : "清除";
				} else if (index == 10) {
					content = 0;
				}
				return content;
			},
			//关闭
			handleClose() {
				if (!this.show) {
					return;
				}
				this.$emit('close', {});
				this.clearD()
			},
			handleClick(e) {
				if (!this.show) {
					return;
				}
				const index = e.currentTarget.dataset.index;
				let numberArr = this.numberArr;
				let pwdArr = this.pwdArr;
				let valArr = this.valArr
				if (index == 9) { //取消键
					this.handleClose();
					return;
				} else if (index == 11) {
					//退格键
					let len = this.numberArr.length;
					pwdArr.splice(len - 1, 1, "");
					valArr.splice(len - 1, 1, "");
					numberArr.pop()
				} else if (index == 10) {
					numberArr.push(0);
					valArr.splice(numberArr.length - 1, 1, 0)
					pwdArr.splice(numberArr.length - 1, 1, "●");
					
				} else {
					numberArr.push(index + 1);
					valArr.splice(numberArr.length - 1, 1, index+1)
					pwdArr.splice(numberArr.length - 1, 1, "●");
				}
				this.numberArr = numberArr;
				this.pwdArr = pwdArr;
				if(this.numberArr.length == this.numberlen){
					this.$emit('complete',this.numberArr)
					this.handleClose()
					return
				}
				this.$emit('click', this.isPwd?pwdArr:valArr)
			},
			clearD: function() {
				this.numberArr = []
				this.pwdArr = []
				this.valArr = []
				for(let i = 0;i<this.numberlen;i++){
					this.pwdArr.push('')
					this.valArr.push('')
				}
			},
		}
	}
</script>
<style scoped>
	@font-face {
		font-family: 'keyboardFont';
		src: url(data:application/font-woff;charset=utf-8;base64,d09GRgABAAAAAASgAA0AAAAABugAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABGRlRNAAAEhAAAABoAAAAch/nJvUdERUYAAARkAAAAHgAAAB4AKQAKT1MvMgAAAZwAAABDAAAAVj4mSapjbWFwAAAB8AAAAD4AAAFCAA/rY2dhc3AAAARcAAAACAAAAAj//wADZ2x5ZgAAAjwAAACsAAAA0BLVU2FoZWFkAAABMAAAAC0AAAA2FXPmsWhoZWEAAAFgAAAAHAAAACQH3gOFaG10eAAAAeAAAAAOAAAAEAwAAABsb2NhAAACMAAAAAoAAAAKAGgAAG1heHAAAAF8AAAAHwAAACABEQBLbmFtZQAAAugAAAFJAAACiCnmEVVwb3N0AAAENAAAACgAAAA6nLlLs3jaY2BkYGAAYukqK754fpuvDNwsDCBwU+tiFBKtwMLA9ABIczAwgUQB4ccH+gAAAHjaY2BkYGBu+N/AEMPCAAJAkpEBFbAAAEcKAm142mNgZG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-weight: normal;
		font-style: normal;
	}

	.tui-icon {
		font-family: "keyboardFont" !important;
		font-size: 22px;
		font-style: normal;
		-webkit-font-smoothing: antialiased;
		-moz-osx-font-smoothing: grayscale;
		line-height: 1;
		color: #333;
	}

	.tui-keyboard-delete:before {
		content: "\e7b8";
	}

	.tui-keyboard-mask {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.6);
		z-index: 998;
		transition: all 0.3s ease-in-out;
		opacity: 0;
		visibility: hidden;
	}

	.tui-mask-show {
		opacity: 1;
		visibility: visible;
	}

	.tui-keyboard {
		width: 100%;
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		z-index: 999;
		padding-bottom: env(safe-area-inset-bottom);
		background-color: #fff;
		border-top: 2rpx solid #f2f2f2;
	}

	.tui-keyboard-radius {
		border-top-left-radius: 16rpx;
		border-top-right-radius: 16rpx;
		overflow: hidden;
	}

	.tui-keyboard-action {
		visibility: hidden;
		transform: translate3d(0, 100%, 0);
		transform-origin: center;
		transition: all 0.3s ease-in-out;
	}

	.tui-keyboard-show {
		transform: translate3d(0, 0, 0);
		visibility: visible;
	}

	.tui-bg-gray {
		background-color: #e7e6eb !important;
	}

	.tui-keyboard-grids {
		width: 100%;
		position: relative;
		overflow: hidden;
		display: flex;
		display: -webkit-flex;
		flex-direction: row;
		flex-wrap: wrap;
	}

	.tui-keyboard-grids::after {
		content: " ";
		position: absolute;
		left: 0;
		top: 0;
		width: 100%;
		height: 1px;
		border-top: 1px solid #eaeef1;
		-webkit-transform-origin: 0 0;
		transform-origin: 0 0;
		-webkit-transform: scaleY(0.5);
		transform: scaleY(0.5);
	}

	.tui-keyboard-grid {
		position: relative;
		padding: 24rpx 20rpx;
		box-sizing: border-box;
		background-color: #fff;
		width: 33.33333333%;
	}

	.tui-keyboard-grid:nth-of-type(3n)::before {
		width: 0;
		border-right: 0;
	}

	.tui-keyboard-grid::before {
		content: " ";
		position: absolute;
		right: 0;
		top: 0;
		width: 1px;
		bottom: 0;
		border-right: 1px solid #eaeef1;
		-webkit-transform-origin: 100% 0;
		transform-origin: 100% 0;
		-webkit-transform: scaleX(0.5);
		transform: scaleX(0.5);
	}

	.tui-keyboard-grid::after {
		content: " ";
		position: absolute;
		left: 0;
		bottom: 0;
		right: 0;
		height: 1px;
		border-bottom: 1px solid #eaeef1;
		-webkit-transform-origin: 0 100%;
		transform-origin: 0 100%;
		-webkit-transform: scaleY(0.5);
		transform: scaleY(0.5);
	}

	.tui-grid-bottom::after {
		height: 0 !important;
		border-bottom: 0 !important;
	}

	.tui-keyboard-hover {
		background-color: #f7f7f9 !important;
	}

	.tui-keyboard-item {
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 48rpx;
		height: 60rpx;
		color: #000;
	}

	.tui-fontsize-32 {
		font-size: 32rpx;
		color: #333 !important;
	}
</style>
